<?php
/*
===========================================================================

  Copyright (c) 2010-2012 DSPWeb Development Team

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see http://www.gnu.org/licenses/

  This file is part of DSPWeb source code

===========================================================================
*/

if (!empty($_POST['create'])) {
	if (empty($_POST['charname']) || empty($_POST['race']) || empty($_POST['gender']) || empty($_POST['hair']) || empty($_POST['face']) || empty($_POST['size']) || empty($_POST['faction']) || empty($_POST['job'])) {
		$error = "One or more required fields were left blank.";
	}
	else {
		$charname = $sql->smartquote($_POST['charname']);
		$race = $sql->smartquote($_POST['race']);
		$gender = $sql->smartquote($_POST['gender']);
		$hair = $sql->smartquote($_POST['hair']);
		$face = $sql->smartquote($_POST['face']);
		$size = $sql->smartquote($_POST['size']);
		$faction = $sql->smartquote($_POST['faction']);
		$job = $sql->smartquote($_POST['job']);
		
		if(!ctype_alpha($charname)) {
			$error = "Character names cannot contain numbers or special characters.";
		}
		elseif (strlen($charname) > 16) {
			$error = "Character name is too long.";
		}
		else {
		
			$query = $sql->query("SELECT * FROM chars WHERE charname='$charname'");
			
			if ($sql->num_rows($query) > 0) {
				$error = "That character name is already being used!";
			}
			else {
		
				// Set the race of the character
			
				switch ($race) {
					case "hume":
						if ($gender == "male") {
							$g = 1;
							break;
						}
						else {
							$g = 2;
							break;
						}
						break;
					case "elvaan":
						if ($gender == "male") {
							$g = 3;
							break;
						}
						else {
							$g = 4;
							break;
						}
						break;
					case "taru":
						if ($gender == "male") {
							$g = 5;
							break;
						}
						else {
							$g = 6;
							break;
						}
						break;
					case "mithra":
						$g = 7;
						break;
					case "galka":
						$g = 8;
						break;
					default:
						$g = 1;
						break;
				}
				
				// Set the size of the character
				
				switch ($size) {
					case "Small":
						$s = 0;
						break;
					case "Medium":
						$s = 1;
						break;
					case "Large":
						$s = 2;
						break;
					default:
						$s = 0;
						break;
				}
				
				// Set the home faction of the character (and set the starting zone for that character)
				
				switch ($faction) {
					case "sandoria":
						$f = 0;
						$zone = 230;
						break;
					case "bastok":
						$f = 1;
						$zone = 235;
						break;
					case "windurst":
						$f = 2;
						$zone = 239;
						break;
					default:
						$f = 0;
						$zone = 230;
					break;
				}
				
				// Set the beginning job of the character
				
				switch ($job) {
					case "war":
						$j = 1;
						break;
					case "mnk":
						$j = 2;
						break;
					case "whm":
						$j = 3;
						break;
					case "blm":
						$j = 4;
						break;
					case "rdm":
						$j = 5;
						break;
					case "thf":
						$j = 6;
						break;
					default:
						$j = 1;
						break;
				}
				
				// Set the look of the character (using face and hair)
				if ($hair == "a") {
					switch ($face) {
						case "1":
							$l = 0;
							break;
						case "2":
							$l = 2;
							break;
						case "3":
							$l = 4;
							break;
						case "4":
							$l = 6;
							break;
						case "5":
							$l = 8;
							break;
						case "6":
							$l = 10;
							break;
						case "7":
							$l = 12;
							break;
						case "8":
							$l = 14;
							break;
						default:
							$l = 0;
							break;
					}
				}
				elseif ($hair == "b") {
					switch($face) {
						case "1":
							$l = 1;
							break;
						case "2":
							$l = 3;
							break;
						case "3":
							$l = 5;
							break;
						case "4":
							$l = 7;
							break;
						case "5":
							$l = 9;
							break;
						case "6":
							$l = 11;
							break;
						case "7":
							$l = 13;
							break;
						case "8":
							$l = 15;
							break;
						default:
							$l = 1;
							break;
					}
				}
				
				// Get the next available character ID
				
				$query = $sql->query("SELECT MAX(charid) FROM chars");
				$res = $sql->fetch_assoc($query);
				$id = $res['MAX(charid)'];
				$id++;
				
				// Process character creation queries
				
				$sql->query("INSERT INTO chars (`charid`,`accid`,`charname`,`nation`,`pos_zone`) VALUES ('$id','".getAccountID($_SESSION['login'])."','$charname','$f','$zone')");
				$sql->query("INSERT into char_look (`charid`,`face`,`race`,`size`) VALUES ('$id','$l','$g','$s')");
				$sql->query("INSERT INTO char_stats (`charid`,`mjob`) VALUES ('$id','$j')");
				
				if(!mysql_error()) {
					$success = true;
				}	
			}
		}
	}
}
$page = "views/create_char.php";
?>